<?php
namespace app\common\model;

use think\Config;
use think\Db;
use think\Model;

class Product extends Model{


    protected $table = "jiujiu_product";



    public function allProducts(){
        $array = [];
        $array['products'] = Db::table('jiujiu_product')
            ->field(['jiujiu_product.*','jiujiu_user.nickname','jiujiu_user.img as headimg'])
            ->join('jiujiu_user','jiujiu_user.uid=jiujiu_product.uid','LEFT')
            ->where('jiujiu_product.isDelete',0)
            ->where('jiujiu_product.isShow',1)
            ->select();
        if(!$array['products']){
            $array['shop'] = null;
            $array['guige'] = null;
        }

        foreach ($array['products'] as $k=> $product){
            $array['shop'][$k] = getUserShop($product['uid']);
            $array['guige'][$k] = model('product_guige')->where('productId',$product['id'])->select();
        }

        return $array;
    }


    public function getShopProduct($owner){

        $shop = model('shop')->getOwnerShop($owner);
        if($shop){
            $data['shop']= $shop;
            $data['product']= model('shop_product')->getShopProduct($shop->id,1);
            return $data;
        }

    }


    public function getShopAllProduct($shopId){
        $array = [];
        $array['products'] =  model('shop_product')->getShopAllProduct($shopId);

        if(!$array['products']){
            $array['guige'] = null;
        }
        foreach ($array['products'] as $k=> $product){
            $array['guige'][$k] = model('product_guige')->where('productId',$product['id'])->select();
        }
        return $array;
    }


    public function getProductByShopId($shopId){
        $data = [];
        $products = model('shop_product')->getShopProduct($shopId);
        foreach ($products as $k=>$product){
           $data['comment'][$k] = model('comment')->getProductCommentCount($product['id'],$shopId);
        }
        $data['products'] = $products;
        return $data;

    }

    public function homeGetShopProduct($owner){
        $shop = model('shop')->getOwnerShop($owner);
        if($shop){
           $products = model('shop_product')->getShopProduct($shop->id);
           return ['product'=>$products,'shop'=>$shop];
        }

    }


    public function homeGetShopProductBySearch($shopId,$keyword){

       $products = model('shop_product')->getShopProductBySearch($shopId,$keyword);
       return $products;


    }





    public function saveProduct($data,$img){

        $array['shopId'] = $data['shopId'];
        unset($data['shopId']);
        $guige = $data['guige'];
        $salePrice = $data['salePrice'];
        $agentPrice = $data['agentPrice'];
        unset($data['guige']);
        unset($data['salePrice']);
        unset($data['agentPrice']);

        if($img){
            $data['img'] = upload($img);
        }

        if(isset($data['id'])){
            $this->where('id',$data['id'])->update($data);
            $id = $data['id'];
        }
        else{
            $data['createTime'] = time();
           $id = $this->insertGetId($data);

           $array['productId'] = $id;
           model('shop_product')->insert($array);
        }
        //规格
        $newGuige = [];
        $i=0;
        foreach ($guige as $k=>$v){
            if(trim($v)){
                $newGuige[$i]['productId'] = $id;
                $newGuige[$i]['guige'] = $guige[$k];
                $newGuige[$i]['salePrice'] = $salePrice[$k];
                $newGuige[$i]['agentPrice'] = $agentPrice[$k];
                $i++;
            }
        }
        if($newGuige){
            model('product_guige')->where('productId',$id)->delete();
            model('product_guige')->insertAll($newGuige) ;
        }

    }


    public function deleteProduct($id){

        $this->where('id',$id)->update(['isDelete'=>1]);

    }


    public function changeShow($i,$v){

        $this->where('id',$i)->update([
            'isShow'=>$v
        ]);

    }




//    商品详情
    public function product_dtl($id,$shopId,$uid){
        $product = $this->where('id',$id)->find();
        $guige = model('product_guige')->where('productId',$id)->select();
        $commentCount = model('comment')->getProductCommentCount($id,$shopId);
        $goodComment = model('comment')->getProductTypeCommentCount($id,$shopId,0);
        if($commentCount){
            $rate = ceil($goodComment/$commentCount) * 100;
        }
        else{
            $rate =  100;
        }
        //收藏
        $isLike = model('like')->userIsLikeProduct($uid,$id,$shopId);
        return [
                'product'=>$product,
                'guige'=>$guige,
                'commentCount'=>$commentCount,
                'rate'=>$rate,
                'isLike'=>$isLike,
        ];
    }


















}